package com.hzspace.wxtemplatemessage;

import com.alibaba.fastjson.JSON;
import com.youzan.cloud.open.sdk.common.exception.SDKException;
import com.youzan.cloud.open.sdk.core.client.auth.Token;
import com.youzan.cloud.open.sdk.core.client.core.DefaultYZClient;
import com.youzan.cloud.open.sdk.core.client.core.YouZanClient;
import com.youzan.cloud.open.sdk.core.oauth.model.OAuthToken;
import com.youzan.cloud.open.sdk.core.oauth.token.TokenParameter;
import com.youzan.cloud.open.sdk.gen.v3_0_0.api.YouzanUserWeixinOpenidGet;
import com.youzan.cloud.open.sdk.gen.v3_0_0.model.YouzanUserWeixinOpenidGetParams;
import com.youzan.cloud.open.sdk.gen.v3_0_0.model.YouzanUserWeixinOpenidGetResult;
import org.springframework.stereotype.Service;

import java.util.concurrent.TimeUnit;

/**
 * TODO
 *
 * @Version 1.0
 * @Author hz
 * @Date 2020/12/2 15:04
 */
@Service
public class YouzanService {

    String clientid = "a2d2af0d32ce27ef5c";
    String clientsecret = "62f9e50e4559c2911c266d853347f21c";
    String grantid = "46248164";


    public String getAccessToken(){
        DefaultYZClient yzClient = new DefaultYZClient();
        TokenParameter tokenParameter;
        try {
            tokenParameter = TokenParameter.self()
                    .clientId(clientid)
                    .clientSecret(clientsecret)
                    .grantId(grantid)
                    .refresh(false)
                    .build();
            OAuthToken oAuthToken = yzClient.getOAuthToken(tokenParameter);
            return JSON.toJSONString(oAuthToken);
        } catch (SDKException e) {
            e.printStackTrace();
        }
        return null;
    }

    public YouzanUserWeixinOpenidGetResult getWxOpenIdByMobileAndYzOpenId(String mobile, String openId, String type) throws SDKException {
        YouZanClient yzClient = new DefaultYZClient();
        Token token = new Token(getAccessToken());
        YouzanUserWeixinOpenidGet youzanUserWeixinOpenidGet = new YouzanUserWeixinOpenidGet();
        //创建参数对象,并设置参数
        YouzanUserWeixinOpenidGetParams params = new YouzanUserWeixinOpenidGetParams();
        params.setCountryCode("+86");
        params.setMobile("15121169425");
        params.setWechatType(1);
        params.setYzOpenId("245098004");
        youzanUserWeixinOpenidGet.setAPIParams(params);
        return yzClient.invoke(youzanUserWeixinOpenidGet, token, YouzanUserWeixinOpenidGetResult.class);
    }


}
